package com.songcha.library_base.compose

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import androidx.compose.foundation.layout.Box
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.ComposeView
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.lifecycleScope
import androidx.lifecycle.repeatOnLifecycle
import com.songcha.library_base.R
import com.songcha.library_base.base.BaseActivity
import com.songcha.library_base.mvvm.base.BaseRepository
import com.songcha.library_base.mvvm.base.BaseViewModel
import kotlinx.coroutines.launch
import java.lang.reflect.ParameterizedType

abstract class BaseComposeMvvmActivity<VM: BaseViewModel<out BaseRepository>>: BaseComposeActivity() {
    lateinit var viewModel: VM


    fun isViewModelInitialized()=::viewModel.isInitialized

    override fun initOnCreate(savedInstanceState: Bundle?) {
        initStatusBar(savedInstanceState)

        //get genericity type
        val cls=(this.javaClass.genericSuperclass as ParameterizedType).actualTypeArguments[0] as Class<VM>
        viewModel= ViewModelProvider(this).get(cls)
        if(mRootView==null) return

        initView(savedInstanceState)
        viewModel.isInit=true

        //setOnApplyWindowInsetsListener()

        initObserver()

        initFlowCollect()

        if(isInitViewGetData())
            onGetData()
    }



    //if getdata on resume(),then no need to getdata on initview()
    protected open fun isInitViewGetData():Boolean{
        return true
    }

    override fun onRetry() {
        onGetData()
    }

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        viewModel.state.observe(this){
            when(it){
                BaseViewModel.LoadState.LOADING->onLoading()
                BaseViewModel.LoadState.LOAD_SUCCESS->onLoadSuccess()
                BaseViewModel.LoadState.LOAD_ERROR->onLoadError(viewModel.error!!)
                BaseViewModel.LoadState.LOAD_NO_NETWORK->onLoadNoNetwork()
                BaseViewModel.LoadState.LOAD_CONNECT_ERROR->onLoadConnectError()
                BaseViewModel.LoadState.LOAD_NO_DATA->onLoadNoData()
                BaseViewModel.LoadState.LOAD_TIMEOUT->onLoadTimeOut()
            }
        }

        viewModel.dialogState.observe(this){
            if(it){
                showLoadingDialog()
            }else{
                hideLoadingDialog()
            }
        }
    }

    protected open fun onGetData(){}

    protected open fun initObserver(){}

    protected open fun initFlowCollect(){}

    protected open fun collectOn(lifecycleSate:Lifecycle.State = Lifecycle.State.STARTED, block:suspend ()->Unit){
        lifecycleScope.launch {
          repeatOnLifecycle(lifecycleSate){
              block()
          }
        }
    }

}